package com.sec.android.app.myfiles.operation.progressboard;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.AppConstants;

import java.util.Locale;

/**
 * Created by daesu83.kim on 2017-04-13.
 */

public class DefaultProgressBoard implements ProgressBoard {

    private int mLayoutResId;

    protected ProgressBar mCurFileProgressBar;
    protected TextView mProcessing;
    protected TextView mCountText;
    protected TextView mPercentText;
    protected TextView mFileNameText;

    public DefaultProgressBoard(int layoutResId) {
        mLayoutResId = layoutResId;
    }

    @Override
    public View ensureView(Context context) {
        View ret = LayoutInflater.from(context).inflate(mLayoutResId, null);
        mCurFileProgressBar = (ProgressBar) ret.findViewById(R.id.cur_file_progressbar);
        mCurFileProgressBar.setMax(100);
        mProcessing = (TextView) ret.findViewById(R.id.file_operation_dialog_progress_status);
        mCountText = (TextView) ret.findViewById(R.id.file_operation_dialog_count);
        mCountText.setText(R.string.preparing);
        mPercentText = (TextView) ret.findViewById(R.id.file_operation_dialog_percent);
        mFileNameText = (TextView) ret.findViewById(R.id.file_name);
        return ret;
    }

    @Override
    public void showProcessingText(boolean show) {
        if (mProcessing != null) {
            mProcessing.setVisibility(show ? View.VISIBLE : View.GONE);
        }
    }

    @Override
    public void setIndeterminateProgress(boolean indeterminate) {
        if (mCurFileProgressBar != null) {
            mCurFileProgressBar.setIndeterminate(indeterminate);
        }
    }

    @Override
    public boolean isIndeterminateProgress() {
        return mCurFileProgressBar != null ? mCurFileProgressBar.isIndeterminate() : false;
    }

    @Override
    public void setProgress(int progress) {
        if (mCurFileProgressBar != null) {
            mCurFileProgressBar.setProgress(progress);
        }
    }

    @Override
    public void setFileNameText(String fileName) {
        if (mFileNameText != null && fileName != null) {
            mFileNameText.setText(fileName);
        }
    }

    @Override
    public void setCountText(int current, int total) {
        if (mCountText != null) {
            if (isIndeterminateProgress() || AppConstants.INDETERMINATE_PROGRESS_START == current) {
                mCountText.setText(R.string.preparing);
            } else {
                mCountText.setText(String.format(Locale.getDefault(), ("%d" + " / " + "%d"),
                        current, total));
            }
        }
    }

    @Override
    public void setCountText(String countText) {
        if (mCountText != null && countText != null) {
            mCountText.setText(countText);
        }
    }

    @Override
    public void setPercentText(String percent) {
        if (mPercentText != null && percent != null) {
            mPercentText.setText(percent);
        }
    }

    @Override
    public void setHandledSize(long handledSize) {
    }

    @Override
    public void clear() {
    }
}